/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.b3log.nide.service;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.cache.Cache;
import org.b3log.latke.cache.CacheFactory;
import org.b3log.latke.service.ServiceException;
import org.b3log.nide.model.NideFile;
import org.b3log.nide.util.parser.Style;
import org.b3log.nide.util.parser.java.NideJavaParser;
import org.json.JSONObject;

/**
 * Java source syntax high lighter.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.3, Mar 10, 2011
 */
public final class JavaSyntaxHighlighter {

    /**
     * Logger.
     */
    private static final Logger LOGGER =
            Logger.getLogger(JavaSyntaxHighlighter.class.getName());
    /**
     * Instance.
     */
    public static final JavaSyntaxHighlighter INSTANCE =
            new JavaSyntaxHighlighter();
    /**
     * Private default constructor.
     */
    private JavaSyntaxHighlighter() {

    }

    /**
     * Gets the highlighting for the specified Java source code at the specified
     * line number and column number.
     *
     * @param javaSourceCode the specified Java source code
     * @param lineNum the specified line number
     * @param columnNum the specified column number
     * @return an highlighting 
     * @throws ServiceException service exception
     */
    public JSONObject getHighlighting(final String javaSourceCode,
                                      final int lineNum, final int columnNum)
            throws ServiceException {

        @SuppressWarnings("unchecked")
        final Cache<String, Object> cache =
                CacheFactory.getCache("javaSousrceCode"); // XXX: cache

        NideJavaParser javaParser = (NideJavaParser) cache.get(javaSourceCode);

        if (null == javaParser) {
            javaParser = new NideJavaParser(javaSourceCode);
            cache.put(javaSourceCode, javaParser);
        }

        final JSONObject ret = new JSONObject();
        try {
            final Style style = javaParser.getStyle(lineNum, columnNum);
            ret.put(NideFile.FILE_STYLE_CHAR_COLOR, style.getColor());
            ret.put(NideFile.FILE_STYLE_CHAR_FONT_WEIGHT, style.getFontWeight());
            ret.put(NideFile.FILE_STYLE_CHAR_FONT_STYLE, style.getFontStyle());
            // TODO: model transformation????
        } catch (final Exception e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
            throw new ServiceException(e);
        }

        return ret;
    }
}
